package ecomm.lib_comm.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Build;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import ecomm.lib_comm.BuildConfig;
import ecomm.lib_comm.permission.DialogProxy;
import ecomm.lib_comm.permission.UsesPermission;

/**
 * 一般应用常用权限,需要拓展参考此类即可
 */
public class PermissionUtil {

    public static void setDialogProxy(DialogProxy proxy) {
        UsesPermission.mDialogProxy = proxy;
    }

    public static void openLocation(final Activity activity, final PermissionListener listener) {

        ArrayList<String> permissions = new ArrayList<>();
        Collections.addAll(permissions
                , Manifest.permission.ACCESS_FINE_LOCATION
                , Manifest.permission.ACCESS_COARSE_LOCATION
        );
        String[] arr = permissions.toArray(new String[0]);
        new UsesPermission(activity, activity, arr) {

            @Override
            protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onComplete(resolvePermissions, lowerPermissions, rejectFinalPermissions, rejectPermissions, invalidPermissions);
                if (resolvePermissions.contains(Manifest.permission.ACCESS_FINE_LOCATION) &&
                        resolvePermissions.contains(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                    LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
                    //获取当前可用的位置控制器
                    List<String> list = locationManager.getProviders(true);
                    boolean flag = false;
                    //检查是否打开了GPS或网络定位
                    if (list.contains(LocationManager.GPS_PROVIDER)) {
                        //是否为GPS位置控制器
                        flag = true;
                    }
                    if (list.contains(LocationManager.NETWORK_PROVIDER)) {
                        //是否为网络位置控制器
                        flag = true;
                    }
                    if (!flag) {
                        if (listener != null) {
                            listener.onPermissionDefined();
                        }
                        return;
                    }
                    listener.onPermissionOk();
                }
            }

            @Override
            protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onFalse(rejectFinalPermissions, rejectPermissions, invalidPermissions);
                listener.onPermissionDefined();
            }

            @Override
            protected String onCancelTips(int viewCancelCount, @NonNull ArrayList<String> permissions, boolean isFinal) {
                listener.onPermissionDefined();
                return super.onCancelTips(viewCancelCount, permissions, isFinal);
            }

            @Override
            protected String onTips(int viewTipsCount, @NonNull ArrayList<String> permissions, boolean isFinal) {
                return listener.tips();
            }
        };

    }

    public static abstract class OnPermissionListener implements PermissionListener {

        public abstract void onPermissionOk();

        @Override
        public void onPermissionDefined() {
        }

        @Override
        public String tips() {
            return null;
        }
    }

    public interface PermissionListener {

        void onPermissionOk();

        void onPermissionDefined();

        String tips();
    }

    /**
     * 常用权限获取 - 拍照
     */
    public static void openCamera(final Activity activity, final PermissionListener listener) {
        ArrayList<String> permissions = new ArrayList<>();
        Collections.addAll(permissions
                , Manifest.permission.CAMERA
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
        );
        String[] arr = permissions.toArray(new String[0]);
        new UsesPermission(activity, activity, arr) {

            @Override
            protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                if (resolvePermissions.contains(Manifest.permission.CAMERA) &&
                        resolvePermissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    listener.onPermissionOk();
                }
            }

            @Override
            protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onFalse(rejectFinalPermissions, rejectPermissions, invalidPermissions);
                Toast.makeText(activity, "授权失败", Toast.LENGTH_SHORT).show();
                listener.onPermissionDefined();
            }
        };
    }

    /**
     * 常用权限获取 - 拍照
     */
    public static void recorderVidoe(final Activity activity, final OnPermissionListener listener) {
        ArrayList<String> permissions = new ArrayList<>();
        Collections.addAll(permissions
                , Manifest.permission.CAMERA
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.RECORD_AUDIO
        );
        String[] arr = permissions.toArray(new String[0]);
        new UsesPermission(activity, activity, arr) {

            @Override
            protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                if (resolvePermissions.contains(Manifest.permission.CAMERA) &&
                        resolvePermissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    listener.onPermissionOk();
                }
            }

            @Override
            protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onFalse(rejectFinalPermissions, rejectPermissions, invalidPermissions);
                Toast.makeText(activity, "授权失败", Toast.LENGTH_SHORT).show();
                listener.onPermissionDefined();
            }
        };
    }

    /**
     * 常用权限获取 - 录音频
     */
    public static void openAudio(final Activity activity, final OnPermissionListener listener) {
        ArrayList<String> permissions = new ArrayList<>();
        Collections.addAll(permissions
                , Manifest.permission.RECORD_AUDIO
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
        );
        String[] arr = permissions.toArray(new String[0]);
        new UsesPermission(activity, activity, arr) {

            @Override
            protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                if (resolvePermissions.contains(Manifest.permission.RECORD_AUDIO) &&
                        resolvePermissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    listener.onPermissionOk();
                }
            }

            @Override
            protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onFalse(rejectFinalPermissions, rejectPermissions, invalidPermissions);
                Toast.makeText(activity, "授权失败", Toast.LENGTH_SHORT).show();
                listener.onPermissionDefined();
            }
        };
    }

    /**
     * 常用权限获取 - 读写操作
     */
    public static void selectLocalFile(final Activity activity, final OnPermissionListener listener) {

        ArrayList<String> permissions = new ArrayList<>();
        Collections.addAll(permissions
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
//                , Manifest.permission.READ_PHONE_STATE
//                , Manifest.permission.INTERNET

        );
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
//            permissions.add(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
//        }
        String[] arr = permissions.toArray(new String[0]);
        new UsesPermission(activity, activity, arr) {

            @Override
            protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                if (resolvePermissions.contains(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    listener.onPermissionOk();
                }
            }

            @Override
            protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
                super.onFalse(rejectFinalPermissions, rejectPermissions, invalidPermissions);
                Toast.makeText(activity, "授权失败", Toast.LENGTH_SHORT).show();
                listener.onPermissionDefined();
            }
        };
    }


}
